/**
 * 掌控力 NG 扩展Frame
 * @param window
 * @param angular
 * @param undefined
 */
(function(window, document, undefined) {'use strict';
app.factory('$z-http', ['$http','z-SessionTimer-s','tokenService', function($http,SessionTimer,tokenService) {
		var zhttp = angular.extend({},$http,{
			post:function(url, data, config) {
				var obj = this.newInstance();
				var tokenData = tokenService(0, false);//读取全局变量
				//增加ran时间戳
				$http.post(url+"?token="+tokenData.token+"&ran="+new Date().getTime(), data, config).success(function(data, status, headers, config) {
					if(data.result==0) {
						if(obj._error) {
							obj._error(data, status, headers, config);
						}
					} else {
						SessionTimer.listenTimeout();
						if(obj._success) {
							obj._success(data, status, headers, config);
						}
					}
				}).error(function(data, status, headers, config) {
					if(SessionTimer.isListening()) {
						if(status == "403") {
							SessionTimer.unlistenTimeout().excuteCallback();
						}else if(status=="404"){
							obj._error(data, status, headers, config);
						}
					} else
					if(obj._error) {
						obj._error(data, status, headers, config);
					}
				});
				return obj;
			},
			get:function(url, config) {
				var obj = this.newInstance();
				var tokenData = tokenService(0, false);//读取全局变量
				$http.get(url+"?token="+tokenData.token+"&ran="+new Date().getTime(), config).success(function(data, status, headers, config) {
					if(data.result==0) {
						if(obj._error) {
							obj._error(data, status, headers, config);
						}
					} else {
						SessionTimer.listenTimeout();
						if(obj._success) {
							obj._success(data, status, headers, config);
						}
					}
				}).error(function(data, status, headers, config) {
					if(SessionTimer.isListening()) {
						if(status == "403") {
							SessionTimer.unlistenTimeout().excuteCallback();
						}
					} else
					if(obj._error) {
						obj._error(data, status, headers, config);
					}
				});
				return obj;
			},
			newInstance:function() {
				var obj = {};
				obj._success = undefined;
				obj._error = undefined;
				obj.success = function(fn) {
					this._success = fn;
					return this;
				},
				obj.error = function(fn) {
					this._error = fn;
					return this;
				}
				return obj;
			}
		});
		return zhttp;
	} ]);

app.service('z-SessionTimer-s',[function() {
	var sessionTimer = {};
	var service = {
		listenTimeout : function(checkSessionFn,delay,callback) {
			if(sessionTimer.isListening) {
				clearInterval(sessionTimer.instance);
			}
			if(checkSessionFn) {
				sessionTimer.checkSessionDelay = delay;
				sessionTimer.checkSessionFn = checkSessionFn;
				sessionTimer.sessionTimeoutCallback = callback;
			}
			if(sessionTimer.checkSessionFn) {
				var that = this;
				sessionTimer.instance = setInterval(function() {
					sessionTimer.checkSessionFn();
				},sessionTimer.checkSessionDelay*1000 + 60000);
				sessionTimer.isListening = true;
			}
			return this;
		},
		unlistenTimeout : function() {
			if(sessionTimer.isListening) {
				clearInterval(sessionTimer.instance);
			}
			sessionTimer.isListening = false;
			return this;
		},
		excuteCallback:function() {
			if(sessionTimer.sessionTimeoutCallback) {
				sessionTimer.sessionTimeoutCallback();
			}
			return this;
		},
		isListening:function() {
			return sessionTimer.isListening;
		}
	}
	return service;
	
} ]);
})();